<template>
    <div class="address-edit">
        <van-nav-bar :title="aid?'修改地址':'新增地址'" left-text="返回" left-arrow fixed @click-left="back" />
        <van-address-edit class="address-edit-content" show-postal show-set-default v-bind:[ishowdel]="true"
            :area-columns-placeholder="['选择省', '选择市', '选择区']" :area-list="areaList"
            :save-button-text="aid?'更改地址':'保存地址'" 
            @save="saveOrupdate" 
            @delete="onDelete"
            :address-info="AddressInfo" />
    </div>
</template>

<script>
    import areaList from '../assets/js/area';
    import '../assets/scss/AddressEdit.scss';
    export default {
        name: "AddressEdit",

        data() {
            return {
                areaList,
                aid: this.$route.params.aid,
                ishowdel: !this.$route.params.aid ? null : 'show-delete',
                AddressInfo: {
                    // key 说明 类型
                    // id 每条地址的唯一标识 number | string
                    // name 收货人姓名 string
                    // tel 收货人手机号 string
                    // province 省份 string
                    // city 城市 string
                    // county 区县 string
                    // addressDetail 详细地址 string
                    // areaCode 地区编码， 通过省市区选择获取（ 必填） string
                    // postalCode 邮政编码 string
                    // isDefault 是否为默认地址 boolean
                }
            };
        },
        created() {
            this.initData();
        },
        methods: {
            //返回上一级
            back() {
                this.$router.go(-1);
            },

            //保存地址
            saveOrupdate(content) {
                console.log('content ==> ', content);

                let url = this.aid ? '/editAddress' : '/addAddress'
                let data = {
                    appkey: this.appkey,
                    tokenString: localStorage.getItem('__tk'),

                    name: content.name,
                    tel: content.tel,
                    province: content.province,
                    city: content.city,
                    county: content.county,
                    addressDetail: content.addressDetail,
                    areaCode: content.areaCode,
                    postalCode: content.postalCode,
                    isDefault: content.isDefault ? 1 : 0
                }
                if(this.aid) data ={...data,aid:this.aid};
                this.axios({
                        //请求类型
                        method: "POST",
                        //请求路径
                        url,
                        //GET请求参数, object
                        // province: 省份,
                        // city: 市,
                        // county: 区县,
                        // addressDetail: 详细地址,
                        // areaCode: 地区编号,
                        // postalCode: 邮政编码,
                        // isDefault: 默认地址
                        data
                    })
                    .then((result) => {
                        this.$toast.clear();
                        this.$toast.success(result.data.msg);
                        console.log("Save|updata result ==> ", result);
                    })
                    .catch((err) => {
                        this.$toast.clear();
                        console.log("err ==> ", err);
                    });
            },
            // 初始化数据
            initData() {
                // findAddressByAid
                if (!this.$route.params.aid) return;
                this.axios({
                    method: 'GET',
                    url: '/findAddressByAid',
                    params: {
                        appkey: this.appkey,
                        tokenString: localStorage.getItem('__tk'),
                        aid: this.aid
                    }
                }).then(result => {

                    if (result.data.code === 40000) {

                        console.log('init result ==>', result);
                        console.log(this.aid);
                        this.AddressInfo = result.data.result[0];
                        console.log(this.AddressInfo)
                        this.AddressInfo.isDefault = !!this.AddressInfo.isDefault;

                    }


                }).catch(err => {
                    this.$toast.fail(err);
                    console.log('err=>', err);
                })
            },
            // 删除已有地址
            onDelete(){
                this.axios({
                    method: 'POST',
                    url: '/deleteAddress',
                    data: {
                        appkey: this.appkey,
                        tokenString: localStorage.getItem('__tk'),
                        aid: this.aid
                    }
                }).then(result => {
                    console.log('del-result',result);
                }).catch(err => {
                    this.$toast.fail(err);
                    console.log('err=>', err);
                })
            }
        },
    };
</script>